﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using BLL;

namespace PhongMachTu.GUI
{
    public partial class frm_LapPhieuKhamBenh : Form
    {
        public frm_LapPhieuKhamBenh()
        {
            InitializeComponent();
            cbo_benhNhan.DataSource = bllBenhNhan.LayDSBenhNhan();
            cbo_benhNhan.ValueMember = "MaBenhNhan";
            cbo_benhNhan.DisplayMember = "HoTen";
        }
        KhambenhBLL KB = new KhambenhBLL();
        KHAMBENH kb = new KHAMBENH();
        public int strMaBenhNhan;
        ThuocBLL bllThuoc = new ThuocBLL();
        DonViBLL bllDonVi = new DonViBLL();
        CachDungBLL bllCachDung = new CachDungBLL();
        LoaibenhBLL bllLoaiBenh = new LoaibenhBLL();
        BenhNhanBLL bllBenhNhan = new BenhNhanBLL();
        ChiTietKhamBenhBLL bllCTKhamBenh = new ChiTietKhamBenhBLL();

        private void frm_LapPhieuKhamBenh_Load(object sender, EventArgs e)
        {


            cbo_benhNhan.SelectedValue = strMaBenhNhan;


            cbo_maLoaiBenh.DataSource = bllLoaiBenh.LayDSLoaiBenh();
            cbo_maLoaiBenh.DisplayMember = "TenLoaiBenh";
            cbo_maLoaiBenh.ValueMember = "MaLoaiBenh";
            cbo_maLoaiBenh.SelectedIndex = -1;


            Thuoc.DataSource = bllThuoc.LayDSThuoc();
            Thuoc.DisplayMember = "TenThuoc";
            Thuoc.ValueMember = "MaThuoc";

            DonVi.DataSource = bllDonVi.LayDSDonVi();
            DonVi.DisplayMember = "TenDonVi";
            DonVi.ValueMember = "MaDonVi";

            CachDung.DataSource = bllCachDung.LayDSCachDung();
            CachDung.DisplayMember = "TenCachDung";
            CachDung.ValueMember = "MaCachDung";


            try
            {
                foreach (var item in KB.LayDSBaoCaoPKB(strMaBenhNhan))
                {
                    cbo_maLoaiBenh.SelectedValue = item.MaLoaiBenh;
                    txt_TrieuChung.Text = item.TrieuChung;
                }
            }
            catch 
            {
                
            
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
     
                KHAMBENH kb = new KHAMBENH();
                kb.MaBenhNhan = strMaBenhNhan;           
                kb.MaLoaiBenh = cbo_maLoaiBenh.SelectedValue.ToString();
                kb.TrieuChung = txt_TrieuChung.Text;
                kb.MaKhamBenh = strMaKhamBenh;

                if (KB.suaKhamBenh(kb))
                {
                    MsgBox.Success("Lập phiếu khám bệnh  thành công  !");
                }
                else
                {
                    MsgBox.Error("Xẩy ra lỗi!");


                }
        }

        private void btn_SoanLai_Click(object sender, EventArgs e)
        {
            try
            {
                kb.MaBenhNhan = Int32.Parse(cbo_benhNhan.Text);
                kb.MaLoaiBenh = cbo_maLoaiBenh.Text;


                KB.suaKhamBenh(kb);
            }
            catch
            {
                MessageBox.Show("Loi roi!");
            }
        }
        public int strMaKhamBenh;
        CHITIETKHAMBENH ct = new CHITIETKHAMBENH();
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["btnSave"].Index && e.RowIndex >= 0)
            {
                try
                {
                    ct.MaThuoc = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                    ct.MaDonVi = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                    int soLuong = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());
                    ct.SoLuong = soLuong;
                    ct.MaCachDung = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();

                    ct.MaKhamBenh = strMaKhamBenh;
                    if (bllCTKhamBenh.ThemChiTietKhamBenh(ct))
                    {
                        MsgBox.Success("Thêm thành công !");
                    }
                    else
                    {
                        MsgBox.Error("Xảy ra lỗi !");
                    }
                }
                catch (Exception)
                {


                }
            }
        }

        private void btnInPhieu_Click(object sender, EventArgs e)
        {
            using (Report.frmPhieuKhamBenh frm = new Report.frmPhieuKhamBenh())
            {
                frm.maBenhNhan = strMaBenhNhan;
                frm.maKhamBenh = strMaKhamBenh;
                foreach (var item in  bllBenhNhan.LayDSBenhNhan(strMaBenhNhan))
                {
                    frm.hoTen = item.HoTen;
                }
                frm.ngayKham = DateTime.Now.ToShortDateString();
                frm.loaiBenh = cbo_maLoaiBenh.Text;
                frm.trieuTrung = txt_TrieuChung.Text;
                frm.ShowDialog();
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            MessageBox.Show(strMaKhamBenh.ToString());
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }

}
